<?php

namespace App\Http\Controllers\Export;

use App\Http\Controllers\Controller;
use App\Models\ResourceTotalAccessNum;
use App\Models\ResourceUserReadBookDay;
use App\Models\UserInfo;
use Maatwebsite\Excel\Facades\Excel;

/**
 * excel导出导入功能  用户统计数据导出
 */
class DataAnalysisExport extends Controller
{

    public $model = null;
    public $limit = 99999;

    public function __construct()
    {
        parent::__construct();

        $this->model = new ResourceTotalAccessNum();
    }

    /**
     * 单本点击量统计排行榜
     * @param start_time 开始时间
     * @param end_time 结束时间
     * @param big_type_id 系统大类
     * @param resource_business_id  资源商id
     * @param page int 当前页
     * @param this->limit int 分页大小
     */
    public function resourceAccessNumStatistics()
    {
        $start_time = request()->start_time;
        $end_time = request()->end_time;
        $big_type_id = request()->big_type_id;
        $resource_business_id = request()->resource_business_id;
        $this->request->merge(['page' => 1]); //重置分页参数

        $data = $this->model->resourceAccessNumStatistics($resource_business_id, $big_type_id, $start_time, $end_time, $this->limit);

        //设置表头
        $row = [[
            "id" => 'ID',
            "big_type_name" => '资源类型',
            "title" => '资源名称',
            "resource_business_name" => '资源商',
            "access_num" => '点击量'
        ]];

        $title = '单本点击量统计排行榜导出-' . date('YmdHis');
        //执行导出
        $data = $this->setData($data['data']); //要导入的数据
        return $this->exportData($data, $title, $row);
    }

    /**
     * 类型点击量统计排行榜
     * @param start_time 开始时间
     * @param end_time 结束时间
     * @param big_type_id 系统大类
     * @param resource_business_id  资源商id
     * @param page int 当前页
     * @param this->limit int 分页大小
     */
    public function resourceTypeStatistics()
    {
        $start_time = request()->start_time;
        $end_time = request()->end_time;
        $big_type_id = request()->big_type_id;
        $resource_business_id = request()->resource_business_id;
        $this->request->merge(['page' => 1]); //重置分页参数

        $data = $this->model->resourceTypeStatistics($resource_business_id, $big_type_id, $start_time, $end_time, $this->limit);
        //设置表头
        $row = [[
            "id" => 'ID',
            "big_type_name" => '资源类型',
            "type_name" => '数据类型',
            "resource_business_name" => '资源商',
            "access_num" => '点击量'
        ]];

        $title = '类型点击量统计排行榜导出-' . date('YmdHis');
        //执行导出
        $data = $this->setData1($data['data']); //要导入的数据
        return $this->exportData($data, $title, $row);
    }


    /**
     * 资源商统计排行榜
     * @param resource_business_id  资源商id
     * @param big_type_id 系统大类
     * @param start_time 开始时间 不传，默认是当天   (时间只对点击量生效)
     * @param end_time 结束时间 不传，默认是当天
     * @param page int 当前页
     * @param this->limit int 分页大小
     */
    public function resourceBusinessStatistics()
    {
        $resource_business_id = request()->resource_business_id;
        $big_type_id = request()->big_type_id;
        $start_time = request()->start_time;
        $end_time = request()->end_time;
        $this->request->merge(['page' => 1]); //重置分页参数

        $data = $this->model->resourceTotalStatistics($resource_business_id, $big_type_id, $start_time, $end_time, $this->limit);
        //设置表头
        $row = [[
            "id" => 'ID',
            "resource_business_name" => '资源商',
            "resource_num" => '资源量',
            "access_num" => '点击量'
        ]];

        $title = '资源量统计导出-' . date('YmdHis');
        //执行导出

        $data = $this->setData2($data['data']); //要导入的数据
        return $this->exportData($data, $title, $row);
    }

    /**
     * 资源类型统计排行榜
     * @param resource_business_id  资源商id
     * @param big_type_id 系统大类
     * @param page int 当前页
     * @param limit int 分页大小
     */
    public function resourceBigTypeStatistics()
    {
        $resource_business_id = request()->resource_business_id;
        // $big_type_id = request()->big_type_id;
        // $start_time = request()->start_time;
        // $end_time = request()->end_time;
        $this->request->merge(['page' => 1]); //重置分页参数

        $data = $this->model->resourceBigTypeStatistics($resource_business_id, 1,$this->limit);
        //设置表头
        $row = [[
            "id" => 'ID',
            "type_name" => '资源类型',
            "resource_num" => '资源量',
            "access_num" => '点击量'
        ]];

        $title = '系统类型统计导出-' . date('YmdHis');
        //执行导出

        $data = $this->setData5($data['data']); //要导入的数据
        return $this->exportData($data, $title, $row);
    }


    /**
     * 用户阅读排行榜
     * @param start_time 开始时间
     * @param end_time 结束时间
     * @param page int 当前页
     * @param this->limit int 分页大小
     */
    public function userReadStatistics()
    {
        $start_time = request()->start_time;
        $end_time = request()->end_time;
        $this->request->merge(['page' => 1]); //重置分页参数

        $resourceUserReadBookDayModel = new ResourceUserReadBookDay();
        $data = $resourceUserReadBookDayModel->userReadStatistics($start_time, $end_time, $this->limit);

        //设置表头
        $row = [[
            "id" => 'ID',
            "serial_number" => '编号',
            "tel" => '账号',
            "nickname" => '昵称',
            "read_duration" => '阅读时长'
        ]];

        $title = '用户阅读排行榜导出-' . date('YmdHis');
        //执行导出

        $data = $this->setData3($data['data']); //要导入的数据
        return $this->exportData($data, $title, $row);
    }

    /**
     * 用户邀请量统计
     * @param start_time 开始时间
     * @param end_time 结束时间
     * @param keywords string 搜索关键词
     * @param page int 当前页
     * @param this->limit int 分页大小
     */
    public function userInviteStatistics()
    {
        $start_time = request()->start_time;
        $end_time = request()->end_time;
        $keywords = request()->keywords;
        $this->request->merge(['page' => 1]); //重置分页参数

        $userInfoModel = new UserInfo();
        $data = $userInfoModel->getInviteUserStatistics($start_time, $end_time, $keywords, $this->limit);

        //设置表头
        $row = [[
            "id" => 'ID',
            "serial_number" => '编号',
            "tel" => '账号',
            "nickname" => '昵称',
            "number" => '邀请量'
        ]];

        $title = '用户邀请量统计导出-' . date('YmdHis');
        //执行导出

        $data = $this->setData4($data['data']); //要导入的数据
        return $this->exportData($data, $title, $row);
    }


    /**
     * 导出数据
     *
     * @param [type] $data
     * @return void
     */
    public function exportData($data, $title, $row)
    {
        $header = $row; //导出表头
        $excel = new Export($data, $header, $title);
        $excel->setColumnWidth(['A' => 10, 'B' => 30, 'C' => 30, 'D' => 20, 'E' => 50, 'F' => 20, 'G' => 20, 'H' => 20, 'I' => 20, 'J' => 20, 'K' => 20, 'L' => 30]);
        $excel->setRowHeight([1 => 30]);
        // $excel->setFreezePane('A2');
        // $excel->setFont(['A1:Z1265' => '宋体']);
        // $excel->setFontSize(['A1:I1' => 14,'A2:Z1265' => 10]);
        // $excel->setBold(['A1:Z2' => true]);
        // $excel->setBackground(['A1:A1' => '808080','C1:C1' => '708080']);
        // $excel->setMergeCells(['A1:I1']);
        // $excel->setBorders(['A2:D5' => '#000000']);
        return Excel::download($excel, $title . '.xlsx');
    }



    public function setData($data)
    {
        $excel_data = [];
        /*设置excel内容*/
        foreach ($data as $key => $val) {
            $excel_data[$key]['id'] = $key + 1;
            $excel_data[$key]['big_type_name'] = $val['big_type_name'];
            $excel_data[$key]['title'] = $val['title'];
            $excel_data[$key]['resource_business_name'] = $val['resource_business_name'];
            $excel_data[$key]['access_num'] = $val['access_num'];
        }
        return $excel_data;
    }


    public function setData1($data)
    {
        $excel_data = [];
        /*设置excel内容*/
        foreach ($data as $key => $val) {
            $excel_data[$key]['id'] = $key + 1;
            $excel_data[$key]['big_type_name'] = $val['big_type_name'];
            $excel_data[$key]['type_name'] = $val['type_name'];
            $excel_data[$key]['resource_business_name'] = $val['resource_business_name'];
            $excel_data[$key]['access_num'] = $val['access_num'];
        }
        return $excel_data;
    }


    public function setData2($data)
    {
        $excel_data = [];
        /*设置excel内容*/
        foreach ($data as $key => $val) {
            $excel_data[$key]['id'] = $key + 1;
            $excel_data[$key]['resource_business_name'] = $val['resource_business_name'];
            $excel_data[$key]['resource_num'] = $val['resource_num'] . ' 个';
            $excel_data[$key]['access_num'] = $val['access_num'];
        }
        return $excel_data;
    }


    public function setData3($data)
    {
        $excel_data = [];
        /*设置excel内容*/
        foreach ($data as $key => $val) {
            $excel_data[$key]['id'] = $key + 1;
            $excel_data[$key]['serial_number'] = $val['serial_number'];
            $excel_data[$key]['tel'] = $val['tel'];
            $excel_data[$key]['nickname'] = $val['nickname'];
            $excel_data[$key]['read_duration'] = $val['read_duration'] . ' 分钟';
        }
        return $excel_data;
    }


    public function setData4($data)
    {
        $excel_data = [];
        /*设置excel内容*/
        foreach ($data as $key => $val) {
            $excel_data[$key]['id'] = $key + 1;
            $excel_data[$key]['serial_number'] = $val['serial_number'];
            $excel_data[$key]['tel'] = $val['tel'];
            $excel_data[$key]['nickname'] = $val['nickname'];
            $excel_data[$key]['number'] = $val['number'];
        }
        return $excel_data;
    }

    public function setData5($data)
    {
        $excel_data = [];
        /*设置excel内容*/
        foreach ($data as $key => $val) {
            $excel_data[$key]['id'] = $key + 1;
            $excel_data[$key]['type_name'] = $val['type_name'];
            $excel_data[$key]['resource_num'] = $val['resource_num'] . ' 个';
            $excel_data[$key]['access_num'] = $val['access_num'];
        }
        return $excel_data;
    }
}
